﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HZ.BidSystem.Model;
using HZ.Data;
using System.Data;

namespace HZ.BidSystem.DAL
{
    using System.Reflection;
    public class DAL_Project_Bid
    {
        /// <summary>
        /// 报价加密报价
        /// </summary>
        /// <param name="bidids"></param>
        /// <param name="encryptprices"></param>
        /// <returns></returns>
        public static int BidPrice(IList<int> bidids, IList<string> encryptprices, IList<int> Azone, IList<int> Bzone, IList<int> Czone, IList<int> Dzone, IList<int> Ezone)
        {
            StringBuilder sbsql = new StringBuilder();
            for (int i = 0; i < bidids.Count; i++)
            {
                sbsql.AppendFormat(HZ.Data.DAL_Helper.GetUpdateSql(new Project_Bid() { BidId = bidids[i], EncryptPrice = encryptprices[i], A_Zone = Azone[i], B_Zone = Bzone[i], C_Zone = Czone[i]}));
            }
            int result = HZ.Data.DbHelperFactory.Create().ExecuteNonQuery(sbsql.ToString());
            return result;
        }

        public static int GetAllGoodsCount(int projectid)
        {
            return (int)HZ.Data.DbHelperFactory.Create().ExecuteScalar(string.Format("SELECT COUNT(BidId) FROM dbo.TB_Project_Bid WHERE ProjectId = {0}", projectid));
        }

        public static int GetBidGoodsCount(int projectid)
        {
            return (int)HZ.Data.DbHelperFactory.Create().ExecuteScalar(string.Format("SELECT COUNT(BidId) FROM dbo.TB_Project_Bid WHERE ProjectId = {0} AND EncryptPrice IS NOT NULL and CompanyId in (select CompanyId from STAT_BidStatus WHERE BidCount > 0 AND ConfirmStatus = 1)", projectid));
        }

        public static int DecPrice(int? CompanyId, int projectid, string pwd1)
        {
            IDbHelper idbhelper = DbHelperFactory.Create();
            var dt = idbhelper.ExecuteTable(string.Format("SELECT BidId, EncryptPrice, DecPrice FROM dbo.TB_Project_Bid WHERE CompanyId = {0} AND ProjectId = {1}", CompanyId.Value, projectid));

            var ie = HZ.Security.EncryptFactory.Instance;
            try
            {
                StringBuilder sqlsb = new StringBuilder(1000);
                int n = 0;

                foreach (DataRow item in dt.Rows)
                {
                    if (!string.IsNullOrEmpty(item["EncryptPrice"].ToString()))
                    {
                        sqlsb.AppendLine(string.Format("UPDATE dbo.TB_Project_Bid SET DecPrice = {0} WHERE BidId = {1}", ie.DecryptString(item["EncryptPrice"].ToString(), pwd1), item["BidId"].ToString()));

                    }
                    if (sqlsb.Length > 1000)
                    {
                        n += HZ.Data.DbHelperFactory.Create().ExecuteNonQuery(sqlsb.ToString());
                        sqlsb = new StringBuilder(1000);
                    }

                }
                if (sqlsb.Length > 0)
                {
                    n += HZ.Data.DbHelperFactory.Create().ExecuteNonQuery(sqlsb.ToString());
                    //idbhelper.ExecuteNonQuery(sqlsb.ToString());
                }

                return n;
            }
            catch (Exception ee)
            {
                // idbhelper.RollTrans();
                HZ.Common.log4netManager.LogDebug("解密报价", "解密报价发生异常", ee);
                return 0;

            }

        }

        public static int GetNoBidCount(int companyid)
        {
            return (int)HZ.Data.DbHelperFactory.Create().ExecuteScalar(string.Format("SELECT COUNT(BidId) FROM dbo.TB_Project_Bid WHERE CompanyId = {0} AND  ISNULL(EncryptPrice,'')=''  ", companyid));
        }

        public static int DecByCenter(int projectid)
        {
            try
            {

                // 检查调用
                string fullname = Assembly.GetCallingAssembly().FullName;
                var strarray = fullname.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                if (strarray.Length == 4)
                {
                    if (strarray[0].Trim() != "HZ.BidSystem.BLL")
                    {
                        throw new Exception("非法系统入口调用");
                    }

                    if (strarray[3].Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries)[1].Trim().ToLower() != "8257763399072e48")
                    {
                        throw new Exception("非法程序集调用");
                    }


                }
                else
                {
                    throw new Exception("程序集全长度有问题！！！");
                }

                DataTable dt = HZ.Data.DbHelperFactory.Create().ExecuteTable("SELECT * FROM  dbo.TB_Project_Bid WHERE EncryptPrice IS NOT NULL ");

                HZ.Security.IEncrypt ie = HZ.Security.EncryptFactory.Instance;
                StringBuilder sbsql = new StringBuilder(8192);
                foreach (DataRow item in dt.Rows)
                {
                    string encyrptprice = item["EncryptPrice"].ToString();
                    if (!string.IsNullOrEmpty(encyrptprice))
                    {
                        string bidid = item["BidId"].ToString();
                        sbsql.AppendLine(string.Format("UPDATE TB_Project_Bid SET DecPrice = {0} WHERE BIdId = {1};", ie.DecryptString(encyrptprice), bidid));
                    }
                    else
                    {
                        continue;
                    }
                    if (sbsql.Length > 8000)
                    {
                        HZ.Data.DbHelperFactory.Create().ExecuteNonQuery(sbsql.ToString());
                        sbsql = new StringBuilder(8000);

                    }
                }
                if (sbsql.Length > 0)
                {
                    HZ.Data.DbHelperFactory.Create().ExecuteNonQuery(sbsql.ToString());
                }
                return 1;

            }
            catch (Exception ee)
            {
                HZ.Common.log4netManager.LogError("集中解密失败", string.Format("集中解密失败操作IP：{0}"), ee);
                throw ee;
            }


        }
    }
}
